package com.android.mmchat.common;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class CompressPicture {
	/**
	 * bitmap是压缩后的图片文件
	 * savePath是压缩后图片的保存路径
	 */
	private Bitmap bitmap;
	private String savePath="/sdcard/feng.jpg";
	
	public Bitmap getBitmap(){
		return bitmap;
	}
	public String getCompressedPicturePath(){
		return savePath;
	}
	
	public void compressWithBounds(String picPath,float width,float heigh){
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		// 获取这个图片的宽和高
		bitmap = BitmapFactory.decodeFile(picPath, options); //此时返回bm为空
		options.inJustDecodeBounds = false;
		int be = Math.round((options.outWidth / width));
		int be2 = Math.round((options.outHeight / heigh));
		if(be<be2) 
			be=be2;
		if (be <= 0)
			be = 1;
		options.inSampleSize = be;
		bitmap=BitmapFactory.decodeFile(picPath,options);
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		System.out.println(w+"   "+h);
		File file=new File(savePath);
		try {
			FileOutputStream out=new FileOutputStream(file);
			if(bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)){
				out.flush();
				out.close();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
